package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.Constant;
import com.example.demo.common.SecurityUtil;
import com.example.demo.common.SessionUtil;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2023-02-14
 * Time: 11:47
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/reg")
    public Object reg(String username,String password) {
        //1.非空效验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return AjaxResult.fail(-1,"非法参数");
        }
        //根据用户名查询用户
        UserInfo userInfo = userService.getUserByName(username);
        if(userInfo!=null && userInfo.getId()>0) {
            //用户名已经存在
            return AjaxResult.fail(-2,"用户名已存在");
        }
        //2.进行添加操作
        int result = userService.add(username, SecurityUtil.encrypt(password));
        if(result==1) {
            return AjaxResult.success(1,"添加成功");
        } else {
            return AjaxResult.fail(-1,"添加失败");
        }
    }

    @RequestMapping("/login")
    public int login(HttpServletRequest request,String username,String password) {
        //1.非空效验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return 0;
        }
        //2.进行数据库查询操作
        UserInfo userInfo = userService.getUserByName(username);
        if(userInfo!=null && userInfo.getId()>0) {
            //用户名正确，验证密码
            if(SecurityUtil.decrypt(password,userInfo.getPassword())) {
                //将userInfo存到session中
                HttpSession session = request.getSession(true);
                session.setAttribute(Constant.SESSION_USERINFO_KEY,userInfo);
                return 1;
            }
        }

        return 0;
    }

    @RequestMapping("/logout")
    public boolean logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session!=null && session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null) {
            session.removeAttribute(Constant.SESSION_USERINFO_KEY);
        }
        return true;
    }

    @RequestMapping("/myinfobyuid")
    public UserInfo getMyInfoByUid(Integer uid) {
        if(uid!=null && uid>0) {
            return userService.getMyInfoByUid(uid);
        }
        return null;
    }

    @RequestMapping("/islogged")
    public int isLogged(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = SessionUtil.getLoginUser(request);
        if(userInfo!=null) {
            return 1;
        }
        response.setStatus(401);
        return 0;
    }
}
